﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using IncrementalBackups.Properties;

namespace IncrementalBackups
{
    static class Program
    {
        private static MainForm _mainScreen;

        public static MainForm MainScreen
        {
            get { return _mainScreen; }
        }
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            System.Windows.Forms.Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            System.Windows.Forms.Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
            _mainScreen = new MainForm();
            if (args.Length > 0)
            {
                foreach (string item in args)
                {
                    if (_mainScreen.ValidRestoreFile(item))
                        break;
                    else
                    {
                        MessageBox.Show("This restore file is not valid", Resources.ProgramName, MessageBoxButtons.OK);
                    }

                }
            }
            Application.Run(_mainScreen);
        }
        /// <summary>
        /// Handles the ThreadException event of the Application. Used to catch unhandled exceptions in main UI thread.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Threading.ThreadExceptionEventArgs"/> instance containing the event data.</param>
        static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            Exception exp = new Exception(string.Format(Resources.ErrorApplicationShutdown, e.Exception.Message), e.Exception);
            Helpers.LogsHelper.WriteToErrorLog(exp, "Program.cs", false);
            if (MainScreen != null && MainScreen.NotifyIcon != null)
                MessageBox.Show(exp.Message, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            System.Windows.Forms.Application.Exit();
        }
    }
}
